﻿namespace ACCOUNTING
{
    using ACCOUNTING.LVY.Business;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;

    public class DBHelper
    {
        public static IDataParameter createParameter(string parameterName, DbType parameterType, ParameterDirection paraDirection, object value)
        {
            switch (((int) modGeneral.DBAccessType))
            {
                case 1:
                    return new OleDbParameter(parameterName, parameterType) { Direction = paraDirection, Value = RuntimeHelpers.GetObjectValue(value) };

                case 2:
                    return new SqlParameter(parameterName, parameterType) { Direction = paraDirection, Value = RuntimeHelpers.GetObjectValue(value) };
            }
            return null;
        }

        public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
        {
            DataSet set=null;
            switch (((int) modGeneral.DBAccessType))
            {
                case 1:
                {
                    OleDbParameter[] parameterArray2 = PrepareOleDbParameter(commandParameters);
                    return OleDBHelper.ExecuteDataset(connectionString, commandType, commandText, parameterArray2);
                }
                case 2:
                {
                    SqlParameter[] parameterArray = PrepareSqlParameter(commandParameters);
                    return SqlHelper.ExecuteDataset(connectionString, commandType, commandText, parameterArray);
                }
            }
            return set;
        }

        public static DataSet ExecuteDataset(string connectionString,string commandText, params IDataParameter[] commandParameters)
        {
            return ExecuteDataset(connectionString,CommandType.Text,commandText,commandParameters);
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
        {
            int num=0;
            switch (((int) modGeneral.DBAccessType))
            {
                case 1:
                {
                    OleDbParameter[] parameterArray2 = PrepareOleDbParameter(commandParameters);
                    return OleDBHelper.ExecuteNonQuery(connectionString, commandType, commandText, parameterArray2);
                }
                case 2:
                {
                    SqlParameter[] parameterArray = PrepareSqlParameter(commandParameters);
                    return SqlHelper.ExecuteNonQuery(connectionString, commandType, commandText, parameterArray);
                }
            }
            return num;
        }

        public static IDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
        {
            IDataReader reader=null;
            switch (((int) modGeneral.DBAccessType))
            {
                case 1:
                {
                    OleDbParameter[] parameterArray2 = PrepareOleDbParameter(commandParameters);
                    return OleDBHelper.ExecuteReader(connectionString, commandType, commandText, parameterArray2);
                }
                case 2:
                {
                    SqlParameter[] parameterArray = PrepareSqlParameter(commandParameters);
                    return SqlHelper.ExecuteReader(connectionString, commandType, commandText, parameterArray);
                }
            }
            return reader;
        }

        public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
        {
            object obj2=null;
            switch (((int) modGeneral.DBAccessType))
            {
                case 1:
                {
                    OleDbParameter[] parameterArray2 = PrepareOleDbParameter(commandParameters);
                    return OleDBHelper.ExecuteScalar(connectionString, commandType, commandText, parameterArray2);
                }
                case 2:
                {
                    SqlParameter[] parameterArray = PrepareSqlParameter(commandParameters);
                    return SqlHelper.ExecuteScalar(connectionString, commandType, commandText, parameterArray);
                }
            }
            return obj2;
        }

        private static OleDbParameter[] PrepareOleDbParameter(params IDataParameter[] commandParameters)
        {
            OleDbParameter[] parameterArray2;
            OleDbParameter[] parameterArray = new OleDbParameter[(commandParameters.Length - 1) + 1];
            try
            {
                int num=0;
                while (num < parameterArray.Length)
                {
                    parameterArray[num] = (OleDbParameter) commandParameters[num];
                    num++;
                }
                parameterArray2 = parameterArray;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw exception;
                ProjectData.ClearProjectError();
            }
            return parameterArray2;
        }

        private static SqlParameter[] PrepareSqlParameter(params IDataParameter[] commandParameters)
        {
            SqlParameter[] parameterArray2;
            SqlParameter[] parameterArray = new SqlParameter[(commandParameters.Length - 1) + 1];
            try
            {
                int num=0;
                while (num < parameterArray.Length)
                {
                    parameterArray[num] = (SqlParameter) commandParameters[num];
                    num++;
                }
                parameterArray2 = parameterArray;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw exception;
                ProjectData.ClearProjectError();
            }
            return parameterArray2;
        }
    }
}

